<template>
<div class="app-container">
  <el-form :inline="true" :model="formList" class="demo-form-inline">
    <el-row>
      <div class="demo-input-suffix">
        <div>
          时间类型：
          <el-select v-model="formList.search_time_type">
            <el-option label="发布时间" value="1"></el-option>
            <el-option label="拼团结束时间" value="2"></el-option>
            <el-option label="拼团开始时间" value="3"></el-option>
          </el-select>
        </div>
        <div>
          <el-date-picker type="date" placeholder="开始时间" v-model="formList.search_begin_time"></el-date-picker>
          <el-date-picker type="date" placeholder="结束时间" v-model="formList.search_end_time"></el-date-picker>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <!-- <el-button class="buttonall" type="primary">批量排单</el-button> -->
        </div>
      </div>
    </el-row>
    <el-row>
      <div class="demo-input-suffix">
        <div>
          <el-select v-model="formList.selectName" @change="clear">
            <el-option label="商家账户" value="1"></el-option>
            <el-option label="商家名称" value="2"></el-option>
            <el-option label="法人名称" value="3"></el-option>
          </el-select>
        </div>
        <div>
          <el-input v-model="formList.order_sn" v-if="formList.selectName === '1'" placeholder="请输入商家账户"></el-input>
          <el-input v-model="formList.service_name" v-if="formList.selectName === '2'" placeholder="请输入商家名称"></el-input>
          <el-input v-model="formList.search_business_name" v-if="formList.selectName === '3'" placeholder="请输入法人名称"></el-input>
        </div>
        <div>
          订单类型:
          <el-select v-model="formList.order_type">
            <el-option label="全部" value=""></el-option>
            <el-option label="拼团订单" value="1"></el-option>
            <el-option label="发布购买订单" value="2"></el-option>
          </el-select>
        </div>
        <div>
          订单状态:
          <el-select v-model="formList.order_status">
            <el-option label="全部" value=""></el-option>
            <el-option label="待支付" value="1"></el-option>
            <el-option label="已取消" value="2"></el-option>
            <el-option label="待成团" value="3"></el-option>
            <el-option label="拼团成功" value="4"></el-option>
            <el-option label="拼团失败" value="5"></el-option>
            <el-option label="退款中" value="6"></el-option>
            <el-option label="退款失败" value="7"></el-option>
            <el-option label="退款成功" value="8"></el-option>
            <el-option label="已分配商户" value="9"></el-option>
            <el-option label="已分配服务人员" value="10"></el-option>
            <el-option label="服务人员已上门" value="11"></el-option>
            <el-option label="确认订单" value="12"></el-option>
            <el-option label="确认开始服务" value="13"></el-option>
            <el-option label="确认服务结束" value="14"></el-option>
            <el-option label="订单完成" value="15"></el-option>
          </el-select>
        </div>
        <div>
          服务人员:
          <el-select v-model="formList.people" placeholder="全部">
            <el-option label="全部" value="all"></el-option>
          </el-select>
        </div>
        <div>
          支付方式:
          <el-select v-model="formList.pay_type">
            <el-option label="全部" value=""></el-option>
            <el-option label="支付宝" value="1"></el-option>
            <el-option label="微信" value="2"></el-option>
          </el-select>
        </div>
      </div>
  </el-row>
  </el-form>
  <el-table :data="tableData" id="out-table">
    <el-table-column type="selection" width="55" align="center"></el-table-column>
    <el-table-column type="index" align="center"></el-table-column>
    <el-table-column
      v-for="item in tableList"
      :key="item._id"
      :prop="item.value"
      :label="item.name"
      align="center">
    </el-table-column>
    <el-table-column label="操作" align="center">
      <template slot-scope="scope">
        <el-button
          size="mini"
          type="primary"
          v-if="scope.row.order_status !== '拼团成功'"
          @click="gotoDetail(scope.$index)">详情</el-button>
        <el-button
          size="mini"
          type="warning"
          v-if="scope.row.order_status === '拼团成功'"
          @click="gotoDetail(scope.$index)">派单</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totals">
    </el-pagination>
    <el-button class="upload" @click="exportExcel">导出excal
    </el-button>
  </div>
</div>
</template>

<script>
  import request from '@/utils/request'
  import { formatDate } from '@/utils/index'
  import { validateOrderStatus,Excel } from '@/utils/validate'
  function getListData(objData) {
    return request({
      url: '/v1/admin/order',
      method: 'post',
      data: objData
    })
  }
  export default {
    data() {
      return {
        formList: {
          page: 1,
          size: 10,
          search_time_type: '1',
          search_begin_time: '',
          search_end_time: '',
          selectName: '1',
          order_sn: '',
          service_name: '',
          search_business_name: '',
          order_type: '',
          order_status: '',
          pay_type: '',
          people: ''
        },
        tableList: [
          { name: '订单编号', value: 'order_sn' },
          { name: '服务标题', value: 'service_title' },
          { name: '服务时间', value: 'service_time' },
          { name: '服务商家', value: 'service_business_name' },
          { name: '订单金额', value: 'order_money' },
          { name: '实付金额', value: 'order_pay_money' },
          { name: '服务区域', value: 'service_personal_area' },
          { name: '服务地址', value: 'service_personal_address' },
          { name: '联系方式', value: 'service_personal_phone' },
          { name: '下单时间', value: 'order_time' },
          { name: '支付方式', value: 'order_pay_type', width: '200px' },
          { name: '第三方交易号', value: 'thirdy_pay_order' },
          { name: '成团时间', value: 'ping_begin_time' },
          { name: '平台派单时间', value: 'send_order_time' },
          { name: '商家派单时间', value: 'business_send_order_time' },
          { name: '服务人员', value: 'service_personal_name' },
          { name: '订单状态', value: 'order_status' },
          { name: '订单类型', value: 'service_time' }
        ],
        tableData: [],
        setTableData: [],
        currentPage: 1,
        size: 10,
        totals: 0
      }
    },
    created() {
      const obj = {
        page: 1,
        size: 10,
      }
      this.getList(obj)
    },
    methods: {
      getList(item) {
        getListData(item).then(res => {
          if (res.code === 200) {
            console.log(res);
            if (res.data.list.length) {
              this.setTableData = res.data.list
              this.totals = res.data.totalNumber
              res.data.list.filter(item => {
                const val = {
                  order_sn: item.order_sn,
                  service_title: item.service_title,
                  service_time: item.service_time ? formatDate(new Date(item.service_time*1000), 'yyyy-MM-dd hh:mm') : '无',
                  service_business_name: item.service_business_name,
                  order_money: item.order_money,
                  order_pay_money: item.order_pay_money,
                  service_personal_area: item.service_personal_id ? item.service_personal_id.service_personal_area:'无',
                  service_personal_address: item.service_personal_id ? item.service_personal_id.service_personal_address:'无',
                  service_personal_phone: item.service_personal_id ? item.service_personal_id.service_personal_phone:'无',
                  order_time: item.order_time ? formatDate(new Date(item.order_time*1000), 'yyyy-MM-dd hh:mm') : '无',
                  order_pay_type: item.order_pay_type,
                  thirdy_pay_order: item.thirdy_pay_order || '无',
                  ping_begin_time: item.service_id ? formatDate(new Date(item.service_id.ping_begin_time*1000), 'yyyy-MM-dd hh:mm') : '无',
                  send_order_time: item.send_order_time ? formatDate(new Date(item.send_order_time*1000), 'yyyy-MM-dd hh:mm') : '无',
                  business_send_order_time: item.business_send_order_time ? formatDate(new Date(item.business_send_order_time*1000), 'yyyy-MM-dd hh:mm') : '无',
                  service_personal_name: item.service_personal_id ? item.service_personal_id.service_personal_name :'无',
                  order_status: validateOrderStatus(item.order_status),
                  order_type: item.order_type
                }
                this.tableData.push(val)
              })
            }
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          }
        }).catch(error => {
          this.$message({
            message: error,
            type: 'error'
          });
        })
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.tableData = []
        let obj = {
          size: val,
          page: 1
        }
        this.getList(obj)
        this.size = val
      },
      handleCurrentChange(page) {
        console.log(`当前页: ${page}`);
        this.tableData = []
        let obj = {
          page: page,
          size: this.size
        }
        this.getList(obj)
      },
      clear() {
        this.formList.order_sn = ''
        this.formList.service_name = ''
        this.formList.search_business_name = ''
      },
      onSubmit() {
        this.tableData = []
        const search_begin_time = this.formList.search_begin_time ? new Date(this.formList.search_begin_time*1000).getTime() : ''
        const search_end_time = this.formList.search_end_time ? new Date(this.formList.search_end_time*1000).getTime() : ''
        const objData ={
          page: this.formList.page,
          size: this.formList.size,
          search_time_type: parseInt(this.formList.search_time_type),
          search_begin_time: search_begin_time,
          search_end_time: search_end_time,
          order_sn: this.formList.order_sn,
          service_name: this.formList.service_name,
          search_business_name: this.formList.search_business_name,
          order_type: parseInt(this.formList.order_type),
          order_status: parseInt(this.formList.order_status),
          pay_type: parseInt(this.formList.pay_type)
        }
        console.log(JSON.stringify(objData))
        this.getList(objData)
      },
      exportExcel() {
        Excel('#out-table')
      },
      gotoDetail(index) {
        localStorage.setItem('orderDetail', JSON.stringify(this.setTableData[index]))
        console.log(this.setTableData[index])
        this.$router.push({
          path: '/orderList/orderDetail'
        })
      }
    },
    filters: {
      isStart: function (value) {
        if (!value||value=='') return '订单详情'
        return value
      }
    }
  }
</script>
<style scoped>
  .el-col{
    margin: 5px;
  }
  .el-row{
    margin: 10px 0;
  }
  .demo-input-suffix{
    display: flex;
    flex-wrap: wrap;
  }
  .demo-input-suffix div{
    margin-right: 10px;
  }
  .el-table{
    margin-bottom: 10px;
  }
  .upload{
    float: right;
  }
  .el-input{
    width: 200px;
  }
  .buttonall{
    float: right;
  }
  .block{
    display: flex;
    justify-content: space-between;
    padding: 10px;
  }
  .el-select{
    width: 160px;
  }
</style>
